package org.example.likou150.array;

/*  动态规划
 *  https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/?envType=study-plan-v2&envId=top-interview-150
 * */
public class MaxProfit {
    public static void main(String[] args) {
        int[] prices = new int[]{7, 1, 5, 3, 6, 4};
        MaxProfit maxProfit = new MaxProfit();
        System.out.println(maxProfit.maxProfit(prices));
    }


    public int maxProfit(int[] prices) {
        int minPrice=Integer.MAX_VALUE;
        int maxPrice=0;
        for(int i=0; i<prices.length;i++){
            if(minPrice>prices[i]){
                minPrice=prices[i];
            }else if(prices[i]-minPrice > maxPrice){
                maxPrice=prices[i]-minPrice;
            }
        }
        return maxPrice;
    }

//    public int maxProfit(int[] prices) {
//        int max = 0;
//        for (int i = 0; i<prices.length; i++) {
//            for (int j=i; j<prices.length; j++){
//                if (prices[i] < prices[j] && prices[j]-prices[i]>max) {
//                    max = prices[j]-prices[i];
//                }
//            }
//        }
//        return max;
//    }
}
